package com.carol.lmsbe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carol.lmsbe.common.Result;
import com.carol.lmsbe.dto.*;
import com.carol.lmsbe.service.EquipmentRepairService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 设备维修控制器
 */
@RestController
@RequestMapping("/api/equipment-repair")
@RequiredArgsConstructor
@Slf4j
public class EquipmentRepairController {

    private final EquipmentRepairService equipmentRepairService;

    /**
     * 分页查询设备维修记录
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<EquipmentRepairDTO>> listRepairRecords(EquipmentRepairQueryRequest request) {
        try {
            Page<EquipmentRepairDTO> result = equipmentRepairService.listRepairRecords(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询设备维修记录失败", e);
            return Result.error("查询设备维修记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取维修记录详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentRepairDTO> getRepairRecordById(@PathVariable Long id) {
        try {
            EquipmentRepairDTO result = equipmentRepairService.getRepairRecordById(id);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取维修记录详情失败", e);
            return Result.error("获取维修记录详情失败: " + e.getMessage());
        }
    }

    /**
     * 创建设备维修记录
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentRepairDTO> createRepairRecord(@Valid @RequestBody EquipmentRepairRequest request) {
        try {
            log.info("收到维修记录创建请求: {}", request);
            log.info("repairCompany: {}", request.getRepairCompany());
            EquipmentRepairDTO result = equipmentRepairService.createRepairRecord(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("创建设备维修记录失败", e);
            return Result.error("创建设备维修记录失败: " + e.getMessage());
        }
    }

    /**
     * 更新设备维修记录
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentRepairDTO> updateRepairRecord(@PathVariable Long id, @Valid @RequestBody EquipmentRepairUpdateRequest request) {
        try {
            request.setId(id);
            EquipmentRepairDTO result = equipmentRepairService.updateRepairRecord(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("更新设备维修记录失败", e);
            return Result.error("更新设备维修记录失败: " + e.getMessage());
        }
    }

    /**
     * 删除设备维修记录
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> deleteRepairRecord(@PathVariable Long id) {
        try {
            equipmentRepairService.deleteRepairRecord(id);
            return Result.success();
        } catch (Exception e) {
            log.error("删除设备维修记录失败", e);
            return Result.error("删除设备维修记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取设备维修统计
     */
    @GetMapping("/statistics")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<List<Map<String, Object>>> getRepairStatistics(EquipmentStatisticsRequest request) {
        try {
            List<Map<String, Object>> result = equipmentRepairService.getRepairStatistics(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取设备维修统计失败", e);
            return Result.error("获取设备维修统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取我的维修记录
     */
    @GetMapping("/my-repairs")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<EquipmentRepairDTO>> getMyRepairRecords(EquipmentRepairQueryRequest request) {
        try {
            Page<EquipmentRepairDTO> result = equipmentRepairService.getMyRepairRecords(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取我的维修记录失败", e);
            return Result.error("获取我的维修记录失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除维修记录
     */
    @DeleteMapping("/batch")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> batchDeleteRepairRecords(@RequestBody List<Long> ids) {
        try {
            equipmentRepairService.batchDeleteRepairRecords(ids);
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除维修记录失败", e);
            return Result.error("批量删除维修记录失败: " + e.getMessage());
        }
    }

    /**
     * 导出维修数据
     */
    @GetMapping("/export")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<byte[]> exportRepairData(EquipmentRepairQueryRequest request) {
        try {
            byte[] result = equipmentRepairService.exportRepairData(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("导出维修数据失败", e);
            return Result.error("导出维修数据失败: " + e.getMessage());
        }
    }
}
